﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ST.Buz.Base.Entity;
using ST.Buz.Base.ViewModel;
using ST.Buz.Service;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using System;
using System.Net;

namespace ST.Buz.Web.Controllers
{
	[ValidateSession]
	[Route("sys/useroauth")]
	public class Sys_UserOAuthController : MyBaseController
	{
		private readonly IServiceSysUserOAuth service;
		public Sys_UserOAuthController(IServiceSysUserOAuth _service,
			   ILogger<Sys_UserOAuthController> _logger) : base(_logger)
		{
			service = _service;
		}

		/// <summary>
		/// 获取当前用户菜单
		/// </summary>
		[HttpPost("oauthlist")]
		public IActionResult GetOAuthListByUser([FromBody]VMS_Sys_UserOAuth vm)
		{
			try
			{
				if (vm == null || vm.userid <= 0)
				{
					throw new ArgumentException("userid不能为空");
				}
				UserSession sessionCurrent = GetSessionFromRequest();

				vm.pnum = 1;
				vm.psize = int.MaxValue;
				var result = service.GetOAuthListByUserID(sessionCurrent, vm);

				return new JsonResultBase(result);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "操作失败", (int)HttpStatusCode.InternalServerError);
			}
		}
	}
}